<?php

namespace Wap\Controller;

use User\Model\DailySignModel;
use Wap\Model\StatisticsModel;
use Wap\Service\WeixinService;
class DailySignController extends WapController
{
    private $_wxUser;
    private $_ak;
    private $_daily_sign;

    public function __construct()
    {
        $this->_ak = 'TVhjYjq1ICT2qqL5LdS8mwas';
        if ($_SERVER['HTTP_HOST'] == '192.168.1.137') {
            $id = I('get.id', -1);
            session('daily_sign_openid_'.$id, 'test');
            session('daily_sign_avatar_'.$id, '');
            session('daily_sign_nickname_'.$id, '游客_test');
            session('daily_sign_sex_'.$id, 0);
            session('daily_sign_city_'.$id, '');
        }
        parent::__construct();
    }

    public function index()
    {
        $id = I('get.id', -1);
        $code = I('get.code', -1);
        $where['id'] = $id;
        $where['status'] = DailySignModel::SIGN_STATUS_ACTIVE;
        $daily_sign = M('daily_sign')->where($where)->find();
        if(empty($daily_sign)){
            header("Content-type: text/html; charset=utf-8");
            die('活动不存在.');
        }
        $this->_daily_sign = $daily_sign;
        $openid = session('daily_sign_openid_'.$id);
        $this->_wxUser = WeixinService::getWxuser($daily_sign['token']);
        if($this->_isWx()){
            if($code != -1){
                if(!$openid){
                    $wechat = new \Wap\Service\WechatService($this->_wxUser['appid'], $this->_wxUser['appsecret']);
                    $out = $wechat->snsapiUserInfo($this->_wxUser['appid'], $this->_wxUser['appsecret'], $code);
                    if($out != null){
                        session('daily_sign_openid_'.$id, $out['openid']);
                        session('daily_sign_avatar_'.$id, $out['headimgurl']);
                        session('daily_sign_nickname_'.$id, $out['nickname']);
                        session('daily_sign_sex_'.$id, $out['sex']);
                        session('daily_sign_city_'.$id, $out['city']);
                    }
                }
                $info = $this->_getInfo($id);
                if(!$info){
                    header("Content-type: text/html; charset=utf-8");
                    die('活动不存在..');
                }
                $this->assign('nickname', session('daily_sign_nickname_'.$id));
                if($info['type'] == DailySignModel::CALENDAR_TYPE_1){
                    $this->assign('avatar', '');
                }else{
                    $this->assign('avatar', $this->_base64Avatar($id));
                }
                $this->assign('wxConfig', $info['wxConfig']);
                $this->assign('wxShare', $info['wxShare']);
                $this->assign('status', $info['status']);
                $this->assign('type', $info['type']);
                $this->assign('word', $info['word']);
                $this->assign('advice_type', $info['advice_type']);
                $this->assign('advice', $info['advice']);
                $this->assign('image', $info['image']);
                $this->assign('year', $info['year']);
                $this->assign('month', $info['month']);
                $this->assign('day', $info['day']);
                $this->assign('has_weather', $info['has_weather']);
                $this->assign('has_festival', $info['has_festival']);
                $this->assign('animal', $info['animal']);
                $this->assign('week', $info['week']);
                $this->assign('festival', $info['festival']);
                $this->assign('lunar', $info['cn_month'].'月'.$info['cn_day']);
                $this->assign('cyclical_month', $info['cyclical_month']);
                $this->assign('cyclical_day', $info['cyclical_day']);
                $this->assign('wind', $info['wind']);
                $this->assign('temperature', $info['temperature']);
                $this->assign('title', $daily_sign['title']);
                $this->assign('qrCode', '/Wap/DailySign/qrCode?id='.$id);
                $this->assign('author', $info['author']);
                StatisticsModel::visit("daily_sign", $id);
                $this->display();
            }else{
                if($openid){
                    $info = $this->_getInfo($id);
                    if(!$info){
                        header("Content-type: text/html; charset=utf-8");
                        die('活动不存在...');
                    }
                    $this->assign('nickname', session('daily_sign_nickname_'.$id));
                    if($info['type'] == DailySignModel::CALENDAR_TYPE_1){
                        $this->assign('avatar', '');
                    }else{
                        $this->assign('avatar', $this->_base64Avatar($id));
                    }
                    $this->assign('wxConfig', $info['wxConfig']);
                    $this->assign('wxShare', $info['wxShare']);
                    $this->assign('status', $info['status']);
                    $this->assign('type', $info['type']);
                    $this->assign('word', $info['word']);
                    $this->assign('advice_type', $info['advice_type']);
                    $this->assign('advice', $info['advice']);
                    $this->assign('image', $info['image']);
                    $this->assign('year', $info['year']);
                    $this->assign('month', $info['month']);
                    $this->assign('day', $info['day']);
                    $this->assign('has_weather', $info['has_weather']);
                    $this->assign('has_festival', $info['has_festival']);
                    $this->assign('animal', $info['animal']);
                    $this->assign('week', $info['week']);
                    $this->assign('festival', $info['festival']);
                    $this->assign('lunar', $info['cn_month'].'月'.$info['cn_day']);
                    $this->assign('cyclical_month', $info['cyclical_month']);
                    $this->assign('cyclical_day', $info['cyclical_day']);
                    $this->assign('wind', $info['wind']);
                    $this->assign('temperature', $info['temperature']);
                    $this->assign('title', $daily_sign['title']);
                    $this->assign('qrCode', '/Wap/DailySign/qrCode?id='.$id);
                    $this->assign('author', $info['author']);
                    StatisticsModel::visit("daily_sign", $id);
                    $this->display();
                }else{
                    $wechat = new \Wap\Service\WechatService($this->_wxUser['appid'], $this->_wxUser['appsecret']);
                    $forward = C('SITE_URL').'/Wap/DailySign/index?'.$_SERVER['QUERY_STRING'];
                    $url = $wechat->snsapiUserInfoLink($forward, $this->_wxUser['appid']);
                    redirect($url);
                }
            }
        }else{
            if(!$openid){
                if(cookie("token")){
                    $openid = cookie("token");
                }else{
                    $openid = $this->_getRandCode();
                    cookie("token", $openid, 60 * 60 * 24 * 30);//一个月
                }
                session('daily_sign_openid_'.$id, $openid);
                session('daily_sign_avatar_'.$id, '');
                session('daily_sign_nickname_'.$id, '游客_'.$openid);
                session('daily_sign_sex_'.$id, 0);
                session('daily_sign_city_'.$id, '');
            }
            $info = $this->_getInfo($id);
            if(!$info){
                header("Content-type: text/html; charset=utf-8");
                die('活动不存在....');
            }
            $this->assign('nickname', session('daily_sign_nickname_'.$id));
            if($info['type'] == DailySignModel::CALENDAR_TYPE_1){
                $this->assign('avatar', '');
            }else{
                $this->assign('avatar', $this->_base64Avatar($id));
            }
            $this->assign('wxConfig', $info['wxConfig']);
            $this->assign('wxShare', $info['wxShare']);
            $this->assign('status', $info['status']);
            $this->assign('type', $info['type']);
            $this->assign('word', $info['word']);
            $this->assign('advice_type', $info['advice_type']);
            $this->assign('advice', $info['advice']);
            $this->assign('image', $info['image']);
            $this->assign('year', $info['year']);
            $this->assign('month', $info['month']);
            $this->assign('day', $info['day']);
            $this->assign('has_weather', $info['has_weather']);
            $this->assign('has_festival', $info['has_festival']);
            $this->assign('animal', $info['animal']);
            $this->assign('week', $info['week']);
            $this->assign('festival', $info['festival']);
            $this->assign('lunar', $info['cn_month'].'月'.$info['cn_day']);
            $this->assign('cyclical_month', $info['cyclical_month']);
            $this->assign('cyclical_day', $info['cyclical_day']);
            $this->assign('wind', $info['wind']);
            $this->assign('temperature', $info['temperature']);
            $this->assign('title', $daily_sign['title']);
            $this->assign('qrCode', '/Wap/DailySign/qrCode?id='.$id);
            $this->assign('author', $info['author']);
            StatisticsModel::visit("daily_sign", $id);
            $this->display();
        }
    }

    //随机生成字符串
    private function _getRandCode()
    {
        $charts = "abcdefghjkmnpqrstuvwxyz0123456789";
        $max = strlen($charts);
        $noncestr = "";
        for($i = 0; $i < 7; $i++)
        {
            $noncestr .= $charts[mt_rand(0, $max)];
        }
        return $noncestr;
    }
    //判断是否微信端
    private function _isWx() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }

    //获取渲染值
    private function _getInfo($id)
    {
        $nickname = session('daily_sign_nickname_'.$id);
        $info = array();
        //签到状态
        if(!$this->_registerUser($id)){
            return false;
        }
        if($this->_doDailySign($id) == 'signed'){
            $status = 2;
        }else{
            $status = 1;
        }
        $info['status'] = $status;
        //日历风格
        $type = $this->_daily_sign['type'];
        $info['type'] = $type;
        //图库、语录库、宜忌文库
        $where_custom['sign_id'] = $id;
        $where_custom['date'] = date('Y-m-d');
        $where_custom['status'] = DailySignModel::CUSTOM_STATUS_ACTIVE;
        $custom = M('daily_sign_custom')->where($where_custom)->order('id desc')->find();
        if(!empty($custom)){
            $word = $custom['word'];
            $author = $custom['author'];
            $advice_type = $custom['advice_type'];
            $advice = $custom['advice'];
            $image = $custom['image'];
        }else{
            $attach_word = $this->_daily_sign['attach_word'];
            $attach_advice = $this->_daily_sign['attach_advice'];
            $attach_image = $this->_daily_sign['attach_image'];
            $word_sort = $this->_daily_sign['word_sort'];
            $advice_sort = $this->_daily_sign['advice_sort'];
            $image_sort = $this->_daily_sign['image_sort'];

            $where_word['attach_id'] = $attach_word;
            $where_word['status'] = DailySignModel::ATTACHMENT_STATUS_ACTIVE;
            if($word_sort == DailySignModel::SORT_1){
                $attachment_word = M('daily_sign_attachment')->where($where_word)->order('id desc')
                    ->field('id,content,author')->find();
            }else{
                $attachment_word = M('daily_sign_attachment')->where($where_word)->order('rand()')
                    ->field('id,content,author')->find();
            }
            $word = $attachment_word['content'];
            $author = $attachment_word['author'];

            $advice_cache = S('daily_sign_advice_'.date('Y-m-d').'_'.$id);
            if($advice_cache){
                $advice_type = $advice_cache['advice_type'];
                $advice = $advice_cache['advice'];
            }else{
                $where_advice['attach_id'] = $attach_advice;
                $where_advice['status'] = DailySignModel::ATTACHMENT_STATUS_ACTIVE;
                if($advice_sort == DailySignModel::SORT_1){
                    $attachment_advice = M('daily_sign_attachment')->where($where_advice)->order('id desc')
                        ->field('id,content,type')->find();
                }else{
                    $attachment_advice = M('daily_sign_attachment')->where($where_advice)->order('rand()')
                        ->field('id,content,type')->find();
                }
                $advice_id = $attachment_advice['id'];
                $advice_type = $attachment_advice['type'];
                $advice = $attachment_advice['content'];
                S('daily_sign_advice_'.date('Y-m-d').'_'.$id, array('id' => $advice_id, 'advice_type' => $advice_type,
                    'advice' => $advice), 24*60*60);
            }

            $image_cache = S('daily_sign_image_'.date('Y-m-d').'_'.$id);
            if($image_cache){
                $image = $image_cache['image'];
            }else{
                $where_image['attach_id'] = $attach_image;
                $where_image['status'] = DailySignModel::ATTACHMENT_STATUS_ACTIVE;
                if($image_sort == DailySignModel::SORT_1){
                    $attachment_image = M('daily_sign_attachment')->where($where_image)->order('id desc')
                        ->field('id,content')->find();
                }else{
                    $attachment_image = M('daily_sign_attachment')->where($where_image)->order('rand()')
                        ->field('id,content')->find();
                }
                $image_id = $attachment_image['id'];
                $image = $attachment_image['content'];
                S('daily_sign_image_'.date('Y-m-d').'_'.$id, array('id' => $image_id, 'image' => $image), 24*60*60);
            }
        }
        $info['word'] = $word;
        $info['author'] = $author;
        $info['advice_type'] = $advice_type;
        $info['advice'] = $advice;
        $info['image'] = $image;
        //分享
        $this->_wxUser = WeixinService::getWxuser($this->_daily_sign['token']);
        $wxJSApi = WeixinService::buildWXJSApi($this->_wxUser);
        $wxConfig =array();
        if(!empty($wxJSApi)){
            $wxConfig = array(
                'appId' => $this->_wxUser['appid'],
                'nonceStr' => $wxJSApi['noncestr'],
                'timestamp' => $wxJSApi['timestamp'],
                'signature' => $wxJSApi['sha1']
            );
        }
        $share_title = $this->_daily_sign['share_title'];
        if(empty($share_title)){
            $share_title = '【时间】【昵称】领取了心情日历签，快来领取你的心情日历签';
        }
        $share_title = str_replace('【时间】', date('Y年m月d'), $share_title);
        $share_title = str_replace('【昵称】', $nickname, $share_title);
        $share_desc = $this->_daily_sign['share_desc'];
        if(empty($share_desc)){
            $share_desc = '【昵称】【宜忌】';
        }
        $share_desc = str_replace('【时间】', date('Y年m月d'), $share_desc);
        $share_desc = str_replace('【昵称】', $nickname, $share_desc);
        if($advice_type == DailySignModel::ATTACHMENT_TYPE_GOOD){
            $share_desc = str_replace('【宜忌】', '宜'.$advice, $share_desc);
        }else{
            $share_desc = str_replace('【宜忌】', '忌'.$advice, $share_desc);
        }
        $share_image = $this->_daily_sign['share_image'];
        if(empty($share_image)){
            $share_image = '/Public/images/daily_sign/share_daily_sign.png';
        }
        $wxShare = array(
            'share_title' => $share_title,
            'share_desc' => $share_desc,
            'share_link' => $this->_daily_sign['share_link'],
            'share_image' => C('SITE_URL').$share_image
        );
        $info['wxConfig'] = $wxConfig;
        $info['wxShare'] = $wxShare;
        //年月日
        $year = date('Y');
        $month = date('n');
        $day = date('j');
        $info['year'] = $year;
        $info['month'] = $month;
        $info['day'] = $day;
        //农历、节日
        $has_weather = $this->_daily_sign['has_weather'];
        $has_festival = $this->_daily_sign['has_festival'];
        $info['has_weather'] = $has_weather;
        $info['has_festival'] = $has_festival;
        $festival = S('daily_sign_festival_'.date('Y-m-d'));
        if(!$festival){
            $festival = $this->_getFestival();
            S('daily_sign_festival_'.date('Y-m-d'), $festival, 24*60*60);
            $animal = $festival['animal'];
            $week = $festival['week'];
            $fe = $festival['festival'];
            $cn_month = $festival['cn_month'];
            $cn_day = $festival['cn_day'];
            $cyclical_month = $festival['cyclical_month'];
            $cyclical_day = $festival['cyclical_day'];
        }else{
            $animal = $festival['animal'];
            $week = $festival['week'];
            $fe = $festival['festival'];
            $cn_month = $festival['cn_month'];
            $cn_day = $festival['cn_day'];
            $cyclical_month = $festival['cyclical_month'];
            $cyclical_day = $festival['cyclical_day'];
        }
        $info['animal'] = $animal;
        $info['week'] = $week;
        $info['festival'] = $fe;
        $info['cn_month'] = $cn_month;
        $info['cn_day'] = $cn_day;
        $info['cyclical_month'] = $cyclical_month;
        $info['cyclical_day'] = $cyclical_day;
        //天气
        $daily_sign_weather = S('daily_sign_weather_'.date('Y-m-d').session('daily_sign_openid_'.$id));
        if(!$daily_sign_weather){
            $weather = $this->_getWeather();
            $wind = $weather['wind'];
            $temperature = $weather['temperature'];
            S('daily_sign_weather_'.date('Y-m-d').session('daily_sign_openid_'.$id), $weather, 24*60*60);
        }else{
            $wind = $daily_sign_weather['wind'];
            $temperature = $daily_sign_weather['temperature'];
        }
        $info['wind'] = $wind;
        $info['temperature'] = $temperature;
        //头像
        $where_avatar['openid'] = session('daily_sign_openid_'.$id);
        $where_avatar['status'] = DailySignModel::USER_STATUS_ACTIVE;
        $avatar = M('daily_sign_user')->where($where_avatar)->getField('avatar');
        $info['avatar'] = $avatar;

        return $info;
    }

    //微信头像转base64
    private function _base64Avatar($id)
    {
        ini_set('default_socket_timeout', 1);
        $url = session('daily_sign_avatar_'.$id);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        curl_close($ch);

        $imgBase64Code = "data:image/jpeg;base64," . base64_encode($package);
        return $imgBase64Code;
    }

    //获取festival数据，增加了CURLOPT_USERAGENT参数
    private function _curlGet($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; 
            c8650 Build/GWK74) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1s');
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }

    //二维码
    public function qrCode()
    {
        $id = I('get.id', -1);
        Vendor('phpqrcode.phpqrcode');
        $url = C('SITE_URL').'/Wap/DailySign/index?id='.$id;
        $level = 'L';
        $size = 4;
        $QRcode = new \QRcode();
        $QRcode::png($url, false, $level, $size, 1);
    }

    //获取ip地址
    private function _getClientIP()
    {
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
                foreach ($arr AS $ip) {
                    $ip = trim($ip);
                    if ($ip != 'unknown') {
                        $realip = $ip;
                        break;
                    }
                }
                if(!isset($realip)){
                    $realip = "0.0.0.0";
                }
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $realip = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                if (isset($_SERVER['REMOTE_ADDR'])) {
                    $realip = $_SERVER['REMOTE_ADDR'];
                } else {
                    $realip = '0.0.0.0';
                }
            }
        } else {
            if (getenv('HTTP_X_FORWARDED_FOR')) {
                $realip = getenv('HTTP_X_FORWARDED_FOR');
            } elseif (getenv('HTTP_CLIENT_IP')) {
                $realip = getenv('HTTP_CLIENT_IP');
            } else {
                $realip = getenv('REMOTE_ADDR');
            }
        }
        preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
        $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
        return $realip;
    }

    //当前城市
    private function _getCity()
    {
        $url = 'http://api.map.baidu.com/location/ip?ak='.$this->_ak.'&ip='.$this->_getClientIP();
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        $city = '';
        if($result['status'] == 0){
            $city = $result['content']['address_detail']['city'];
        }
        return $city;
    }

    //当前天气
    private function _getWeather()
    {
        $city = $this->_getCity();
        $weather = '';
        if($city != ''){
            $url = 'http://api.map.baidu.com/telematics/v3/weather?location='.$city.'&output=json&ak='.$this->_ak;
            $result = file_get_contents($url);
            $result = json_decode($result, true);
            if($result['status'] == 'success'){
                $weather = $result['results'][0]['weather_data'][0];
            }
        }
        return $weather;
    }

    //农历，节日
    private function _getFestival()
    {
        $url = 'https://www.sojson.com/open/api/lunar/json.shtml';
        $result = $this->_curlGet($url);
        $result = json_decode($result, true);
        $festival = '';
        if($result['status'] == 200){
            $festival = array(
                'animal' => $result['data']['animal'],
                'week' => $this->_cnWeek($result['data']['week']),
                'festival' => $result['data']['festivalList'][0],
                'cn_month' => $result['data']['cnmonth'],
                'cn_day' => $result['data']['cnday'],
                'cyclical_month' => $result['data']['cyclicalMonth'].'月',
                'cyclical_day' => $result['data']['cyclicalDay'].'日'
            );
        }
        return $festival;
    }

    //外国星期转中文
    private function _cnWeek($week)
    {
        $w = array(
            'Monday' => '星期一',
            'Tuesday' => '星期二',
            'Wednesday' => '星期三',
            'Thursday' => '星期四',
            'Friday' => '星期五',
            'Saturday' => '星期六',
            'Sunday' => '星期日'
        );
        return $w[$week];
    }

    //记录用户
    private function _registerUser($id)
    {
        $openid = session('daily_sign_openid_'.$id);
        if(!$openid || $openid == ''){
            return false;
        }
        $nickname = session('daily_sign_nickname_'.$id);
        $avatar = session('daily_sign_avatar_'.$id);
        $city = session('daily_sign_city_'.$id);
        $where['sign_id'] = $id;
        $where['openid'] = $openid;
        $where['status'] = array('neq', DailySignModel::USER_STATUS_DELETE);
        $user = M('daily_sign_user')->where($where)->find();
        if(empty($user)){
//            //保存头像
//            $name = session('daily_sign_openid_' . $id) . ".jpg";
//            $save_path = "Public/Uploads/wx_visit_head/daily_sign/" . $id . "/";
//            if (!is_dir($save_path)) mkdir($save_path);
//            $file = $save_path . $name;
//            if (!file_exists($file)) {
//                //1、微信头像地址处理
//                $new_headurl = $this->_newHeadUrl($avatar);
//                //2、下载头像图片
//                $this->_downloadWxHead($new_headurl, $file);
//            }
//            $headurl = "/" . $file;
            $data = array(
                'sign_id' => $id,
                'openid' => $openid,
                'nickname' => json_encode($nickname),
                'avatar' => $avatar,
                'city' => $city,
                'status' => DailySignModel::USER_STATUS_ACTIVE
            );
            M('daily_sign_user')->add($data);
            return true;
        }else{
            if($user['status'] == DailySignModel::USER_STATUS_BLACK){
                return false;
            }
        }
        return true;
    }

    private function _newHeadUrl($headurl)
    {
        $new_headurl = "";
        if (!empty($headurl)) {
            $headurl_arr = explode("/", $headurl);
            $new_headurl = "";
            if (!empty($headurl_arr)) {
                array_pop($headurl_arr);
                foreach ($headurl_arr as $key_h_arr => $value_h_arr) {
                    $new_headurl = $new_headurl . $value_h_arr . "/";
                }
            }
            $new_headurl = $new_headurl . "132";
        }
        return $new_headurl;
    }

    private function _downloadWxHead($new_headurl, $file)
    {
        $ch = curl_init($new_headurl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        curl_close($ch);

        $local_file = fopen($file, 'w');
        fwrite($local_file, $package);
        fclose($local_file);
    }

    //用户签到
    private function _doDailySign($id)
    {
        $openid = session('daily_sign_openid_'.$id);
        if(!$openid){
            return 'unsigned';
        }
        $where['id'] = $id;
        $where['status'] = DailySignModel::SIGN_STATUS_ACTIVE;
        $daily_sign = M('daily_sign')->where($where)->find();
        if(!empty($daily_sign)){
            $day_start = date('Y-m-d') . ' 00:00:00';
            $day_end = date('Y-m-d') . ' 23:59:59';
            $where_record['sign_id'] = $id;
            $where_record['openid'] = $openid;
            $where_record['createdate'] = array('between', array($day_start, $day_end));
            $where_record['status'] = DailySignModel::RECORD_STATUS_ACTIVE;
            $record = M('daily_sign_record')->where($where_record)->find();
            if(empty($record)){
                $data = array(
                    'sign_id' => $id,
                    'openid' => $openid
                );
                M('daily_sign_record')->add($data);
                return 'unsigned';
            }else{
                return 'signed';
            }
        }else{
            return 'unsigned';
        }
    }
}